<template>
  <div class="tomorrow-classes">
    <div class="tomorrow-header">
      <h3 class="section-title">
        <el-icon><Calendar /></el-icon>
        明日课程安排 ({{ tomorrowDayName }})
      </h3>
    </div>

    <div v-if="loading" class="loading-container">
      <el-skeleton :rows="2" animated />
    </div>

    <div v-else-if="shouldShowRestMessage" class="no-classes">
      <div :class="restMessageClass">
        <el-icon size="40"><Sunny /></el-icon>
        <p class="rest-text">{{ restMessage.text }}</p>
        <p class="rest-subtitle">{{ restMessage.subtitle }}</p>
      </div>
    </div>

    <div v-else class="classes-content">
      <div
        v-for="(classItem, index) in tomorrowClasses"
        :key="classItem.id"
        class="class-item"
      >
        <div class="class-time">
          {{ formatTime(classItem.startTime) }}
        </div>
        <div class="class-name">
          {{ classItem.subject }}
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { Calendar, Sunny } from '@element-plus/icons-vue'
import { scheduleApi } from '@/api/schedule'
import dayjs from 'dayjs'
import 'dayjs/locale/zh-cn'

dayjs.locale('zh-cn')

const props = defineProps({
  studentId: {
    type: Number,
    required: true
  }
})

const loading = ref(false)
const tomorrowClasses = ref([])

// 计算明天是周几
const tomorrowDayName = computed(() => {
  const tomorrow = dayjs().add(1, 'day')
  const dayOfWeek = tomorrow.day() // 0-6, 0=周日
  const dayNames = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
  return dayNames[dayOfWeek]
})

// 计算明天是否是周末
const isTomorrowWeekend = computed(() => {
  const tomorrow = dayjs().add(1, 'day')
  const dayOfWeek = tomorrow.day() // 0-6, 0=周日
  return dayOfWeek === 0 || dayOfWeek === 6 // 周日或周六
})

// 计算是否需要显示休息消息
const shouldShowRestMessage = computed(() => {
  return tomorrowClasses.value.length === 0
})

// 计算休息消息内容
const restMessage = computed(() => {
  console.log('DEBUG: isTomorrowWeekend.value =', isTomorrowWeekend.value)
  console.log('DEBUG: tomorrowClasses.value.length =', tomorrowClasses.value.length)

  if (isTomorrowWeekend.value) {
    console.log('DEBUG: 显示周末消息')
    return {
      text: "明天是周末，好好休息吧~",
      subtitle: "记得合理安排作息时间哦！"
    }
  } else {
    console.log('DEBUG: 显示工作日无课程消息')
    return {
      text: "明天暂无课程安排",
      subtitle: "可以利用时间复习或预习哦！"
    }
  }
})

// 计算休息消息样式类
const restMessageClass = computed(() => {
  return isTomorrowWeekend.value ? "rest-message" : "no-classes-message"
})

// 格式化时间
const formatTime = (time) => {
  if (!time) return ''
  return dayjs(`2023-01-01T${time}`).format('HH:mm')
}

// 加载明日课程
const loadTomorrowClasses = async () => {
  if (!props.studentId) {
    return
  }

  try {
    loading.value = true
    const response = await scheduleApi.getTomorrowClasses(props.studentId)
    tomorrowClasses.value = response.data || []
  } catch (error) {
    console.error('加载明日课程失败:', error)
    ElMessage.error('加载明日课程失败')
    tomorrowClasses.value = []
  } finally {
    loading.value = false
  }
}

// 刷新数据
const refresh = () => {
  loadTomorrowClasses()
}

// 暴露刷新方法给父组件
defineExpose({
  refresh
})

// 监听studentId的变化，当studentId有效时重新加载数据
watch(() => props.studentId, (newStudentId) => {
  console.log('DEBUG: studentId changed to:', newStudentId)
  if (newStudentId && !isNaN(newStudentId)) {
    loadTomorrowClasses()
  }
}, { immediate: false })

onMounted(() => {
  // 只有当studentId有效时才加载
  if (props.studentId && !isNaN(props.studentId)) {
    loadTomorrowClasses()
  } else {
    console.log('DEBUG: studentId is invalid on mounted:', props.studentId)
  }
})
</script>

<style scoped>
.tomorrow-classes {
  background: white;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.tomorrow-header {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}

.section-title {
  font-size: 18px;
  font-weight: bold;
  color: #333;
  margin: 0;
  display: flex;
  align-items: center;
  gap: 8px;
}

.loading-container {
  padding: 20px;
}

.no-classes {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 200px;
  text-align: center;
}

.rest-message {
  color: #67c23a;
}

.no-classes-message {
  color: #e6a23c;
}

.rest-text {
  font-size: 18px;
  font-weight: bold;
  margin: 16px 0 8px 0;
}

.rest-subtitle {
  font-size: 14px;
  color: #999;
  margin: 0;
}

.classes-content {
  /* 移除高度限制和滚动条 */
}

.class-item {
  display: flex;
  align-items: center;
  padding: 8px 12px;
  margin-bottom: 8px;
  background: #f8f9fa;
  border-radius: 6px;
  border-left: 3px solid #409eff;
}

.class-item:last-child {
  margin-bottom: 0;
}

.class-time {
  min-width: 60px;
  font-size: 14px;
  font-weight: 500;
  color: #409eff;
}

.class-name {
  flex: 1;
  margin-left: 16px;
  font-size: 15px;
  font-weight: 500;
  color: #333;
}

@media (max-width: 768px) {
  .class-item {
    padding: 6px 10px;
  }

  .class-time {
    min-width: 50px;
    font-size: 13px;
  }

  .class-name {
    font-size: 14px;
  }
}
</style>